<?php
// 运营管理
namespace app\admin\controller;

use app\admin\controller\Base;
use app\common\model\Document;
use app\common\model\SystemSet;
use think\Db;
use think\cache\driver\Redis;
use app\common\model\HuiYuanSupervise;
class Operate extends Base
{

    public $document;

    public function _initialize()
    {
        parent::_initialize();
        $this->member_model= new HuiYuanSupervise();
        $this->document = new Document();
        $this->systemSet = new SystemSet();
    }

    /**
     * 保存图片并获取路径
     * 
     * @return string
     */
    public function getImgUrl()
    {
        return $this->systemSet->uploadUrl(input('post.name'));
    }

    /**
     * 电脑版广告
     * 
     * @return \think\mixed
     */
    public function yx_dngg()
    {
        $lunbo = $this->document->DocumentList('dngg', 'gg_class asc');
        $this->assign('lunbo', $lunbo);
        return $this->fetch();
    }

    public function PublicAdvertising($table, $count)
    {
        for ($i = 1; $i <= $count; $i ++) {
            $file = $this->systemSet->uploadUrl('file_' . $i);
            $lunboggim = input('post.lunbo_url' . $i) ? input('post.lunbo_url' . $i) : '#';
            $ifcz = $this->document->DocumentFind($table, array(
                'gg_class' => $i
            ), 'gg_class asc');
            if ($ifcz['id'] != '') {
                if ($file != '') {
                    $value = array(
                        'gg_img' => $file,
                        'gg_url' => $lunboggim,
                        'gg_class' => $i
                    );
                } else {
                    $value = array(
                        'gg_url' => $lunboggim,
                        'gg_class' => $i
                    );
                }
                $where = array(
                    'gg_class' => $i
                );
                $this->document->EditDocument($table, $where, $value);
            } else {
                $value = array(
                    'gg_img' => $file,
                    'gg_url' => $lunboggim,
                    'gg_class' => $i
                );
                $this->document->AddDocument($table, $value);
            }
        }
        if($count==6){
            $type="电脑版";
        }
        if($count==5){
            $type="手机版";
        }
        $this->member_model->caozuorizhi_xieru('修改'.$type.'广告');
        $this->success('广告修改成功','/admin/Operate/yx_dngg');
    }

    /**
     * 电脑版广告添加
     * 
     * @param 【表名】 $table            
     * @param 【条数】 $count            
     */
    public function AddPcAdvertising()
    {
        $this->PublicAdvertising('dngg', 6);
    }

    /**
     * 手机版广告
     * 
     * @return \think\mixed
     */
    public function yx_sjgg()
    {
        $lunbo = $this->document->DocumentList('sjgg', 'gg_class');
        $this->assign('lunbo', $lunbo);
        return $this->fetch();
    }

    /**
     * 手机端广告添加
     * 
     * @param 【表名】 $table            
     * @param 【条数】 $count            
     */
    public function AddMobileAdvertising()
    {
        $this->PublicAdvertising('sjgg', 5);
    }

    /**
     * 公告列表
     * 
     * @return \think\mixed
     */
    public function yx_gglist()
    {
        $list = $this->document->DocumentList('xt_gonggao', 'id desc');
        $this->assign('list', $list);
        return $this->fetch();
    }

    /**
     * 删除公告
     * 
     * @param 公告id $id            
     */
    public function DelAnnouncement($id)
    {
        $gonggao=Db::name('xt_gonggao')->where('id',$id)->find();
        if ($this->document->DelAnnouncement('xt_gonggao', $id)){
            $list = Db::name('xt_gonggao')->where('suggest_web',1)->field('id,title,ggtext')->order('fatime desc')->select();
            $redis = new Redis();
            $redis->set('sign',json_encode($list));
            
            $this->member_model->caozuorizhi_xieru('删除公告《'.$gonggao['title'].'》');
            $this->success('删除成功');
        }else{
            $this->error('删除失败');
        }
    }

    /**
     * 公告修改
     * 
     * @param 【公告id】 $id            
     * @return \think\mixed
     */
    public function yx_ggxg($id)
    {
        if (input('post.submit')) {
            $value = array(
                'title' => input('post.NEW_TITLE'),
                'ggtext' => input('post.NEW_GGTEXT'),
                'xianshi' => input('post.xianshi'),
                'fatime' => strtotime(input('post.NEW_FATIME'))
            );
            $update = $this->document->EditDocument('xt_gonggao', array(
                'id' => $id
            ), $value);
            $gonggao=Db::name('xt_gonggao')->where('id',$id)->find();
            if ($update) {
                $list = Db::name('xt_gonggao')->where('suggest_web',1)->field('id,title,ggtext')->order('fatime desc')->select();
                $redis = new Redis();
                $redis->set('sign',json_encode($list));
                $this->member_model->caozuorizhi_xieru('修改公告《'.$gonggao['title'].'》');
                $this->success('修改成功', '/admin/operate/yx_gglist');
            } else {
                $this->error('修改失败');
            }
        } else {
            $DLZL = $this->document->DocumentFind('xt_gonggao', array(
                'id' => $id
            ));
            $this->assign('DLZL', $DLZL);
            return $this->fetch();
        }
    }

    /**
     * 公告是否轮播
     * 
     * @param 【公告id】 $id            
     */
    public function yx_gglb($id)
    {
        $find = $this->document->DocumentFind('xt_gonggao', array(
            'id' => $id
        ));
        if ($find['xianshi'] == 1) {
            $this->document->EditDocument('xt_gonggao', array(
                'id' => $id
            ), array(
                'xianshi' => 2
            ));
            $this->member_model->caozuorizhi_xieru('将公告《'.$find['title'].'》设置为:轮播不显示');
        } else {
            $this->document->EditDocument('xt_gonggao', array(
                'id' => $id
            ), array(
                'xianshi' => 1
            ));
            $this->member_model->caozuorizhi_xieru('将公告《'.$find['title'].'》设置为:轮播显示');
        }
        $this->redirect('/admin/operate/yx_gglist');
    }

    //公告发布
    public function yx_ggfb($id,$fb){
        $find = $this->document->DocumentFind('xt_gonggao', array('id' => $id));
        if( 'roll'==$fb ){
            $this->document->EditDocument('xt_gonggao', array('id' => $id),array('is_roll'=>1-intval($find['is_roll'])));
            $this->member_model->caozuorizhi_xieru('将公告《'.$find['title'].'》,滚动状态设置为:'.(1-intval($find['is_roll'])));
        }else{
            $suggest = $find['suggest_'.$fb] == 0 ? 1 : 0;
            if($fb=='web'){$shebei="web端首页推荐";}
            if($fb=='mobile'){$shebei="手机端首页推荐";}
            if($suggest==0){$zhuangtai="否";}
            if($suggest==1){$zhuangtai="是";}
            $this->document->EditDocument('xt_gonggao', array('id' => $id),array('suggest_'.$fb=>$suggest));
            $this->member_model->caozuorizhi_xieru('将公告《'.$find['title'].'》'.$shebei.'设置为:'.$zhuangtai);
        }
        $list = Db::name('xt_gonggao')->where('suggest_web',1)->field('id,title,ggtext')->order('fatime desc')->select();
        $redis = new Redis();
        $redis->set('sign',json_encode($list));
        $this->redirect('/admin/operate/yx_gglist');
    }
    
    
    /**
     * 公告排序修改
     */
    public function yx_ggpx()
    {
        $list = $this->document->DocumentList('xt_gonggao', 'id desc');
        foreach ($list as $val) {
            $value = array(
                'paixu' => input('post.GGPX_' . $val['id'])
            );
            $this->document->EditDocument('xt_gonggao', array(
                'id' => $val['id']
            ), $value);
        }
        $this->member_model->caozuorizhi_xieru('修改公告排序');
        $this->redirect('/admin/operate/yx_gglist');
    }

    /**
     * 添加公告
     */
    public function yx_ggnew()
    {
        $time = date('Y-m-d H:i:s');
        $this->assign('time', $time);
        if (input('post.submit')) {
            $value = array(
                'title' => input('post.NEW_TITLE'),
                'ggtext' => input('post.NEW_GGTEXT'),
                'fatime' => strtotime(input('post.NEW_FATIME')) ? strtotime(input('post.NEW_FATIME')) : time() ,
                'faren' => input('cookie.user'),
                'xianshi' => input('post.xianshi'),
                'paixu' => 0
            );
            if ($this->document->AddDocument('xt_gonggao', $value)) {
                $list = Db::name('xt_gonggao')->where('suggest_web',1)->field('id,title,ggtext')->order('fatime desc')->select();
                $redis = new Redis();
                $redis->set('sign',json_encode($list));
                $this->member_model->caozuorizhi_xieru('新增一条公告');
                $this->success('添加成功', '/admin/operate/yx_ggnew');
            } else {
                $this->error('添加失败');
            }
        } else {
            return $this->fetch();
        }
    }

    /**
     * 优惠列表
     * 
     * @return \think\mixed
     */
    public function yx_yhlist()
    {
        $list = $this->document->DocumentList('xt_yhhd_list', 'id desc');
        $this->assign('list', $list);
        return $this->fetch();
    }

    /**
     * 优惠修改
     * 
     * @return string|\think\mixed
     */
    public function yx_yhxg()
    {
        if (input('post.submit')) {
            $value = array(
                'title' => input('post.title'),
                'pic' => input('post.pic'),
                'desc' => input('post.desc'),
                'content' => input('post.content'),
                'begin_time' => input('post.begin_time'),
                'end_time' => input('post.end_time'),
                'issuer' => input('cookie.user'),
                'xianshi' => 1
            );
            if ($this->document->EditDocument('xt_yhhd_list', array(
                'id' => input('get.id')
            ), $value)) {
                $this->success('修改成功', '/admin/operate/yx_yhlist');
            } else {
                $this->error('修改失败');
            }
        }
        $DLZL = $this->document->DocumentFind('xt_yhhd_list', array(
            'id' => input('get.id')
        ));
        $this->assign('DLZL', $DLZL);
        return $this->fetch();
    }

    /**
     * 优惠删除
     * 
     * @param 【优惠活动id】 $id            
     */
    public function yx_yhdel($id)
    {
        if ($this->document->DelAnnouncement('xt_yhhd_list', $id)) {
            $this->member_model->caozuorizhi_xieru('删除一条公告');
            $this->success('删除成功');
        } else {
            $this->error('删除失败');
        }
    }

    /**
     * 优惠排序
     */
    public function yx_yhpx()
    {
        $list = $this->document->DocumentList('xt_yhhd_list', 'id desc');
        foreach ($list as $val) {
            $value = array(
                'paixu' => input('post.GGPX_' . $val['id'])
            );
            $this->document->EditDocument('xt_yhhd_list', array(
                'id' => $val['id']
            ), $value);
        }
        $this->redirect('/admin/operate/yx_yhlist');
    }

    /**
     * 新增优惠
     * 
     * @return \think\mixed
     */
    public function yx_yhnew()
    {
        if (input('post.submit')) {
            $value = array(
                'title' => input('post.title'),
                'pic' => input('post.pic'),
                'desc' => input('post.desc'),
                'content' => input('post.content') ? input('post.content') : '',
                'begin_time' => input('post.begin_time'),
                'end_time' => input('post.end_time'),
                'issuer' => input('cookie.user'),
                'xianshi' => 1
            );
            if ($this->document->AddDocument('xt_yhhd_list', $value)) {
                $this->success('添加成功', '/admin/operate/yx_yhnew');
            } else {
                $this->error('添加失败');
            }
        } else {
            $this->assign('cur_time',date('Y-m-d',time()));
            return $this->fetch();
        }
    }

    /**
     * 新闻列表
     * 
     * @return \think\mixed
     */
    public function yx_xwlist()
    {
        $list = $this->document->DocumentList('xt_news', 'id desc');
        $this->assign('list', $list);
        return $this->fetch();
    }

    /**
     * 新闻修改
     * 
     * @return string|\think\mixed
     */
    public function yx_xwxg()
    {
        if (input('post.submit')) {
            $value = array(
                'type' => input('post.NEW_TYPE'),
                'title' => input('post.NEW_TITLE'),
                'ggtext' => input('post.NEW_GGTEXT'),
                'fatime' => input('post.NEW_FATIME') ? strtotime(input('post.NEW_FATIME')) : time()
            );
            if ($this->document->EditDocument('xt_news', array(
                'id' => input('get.id')
            ), $value)) {
                $this->member_model->caozuorizhi_xieru('修改新闻《'.input('post.NEW_TITLE').'》');
                $this->success('修改成功', '/admin/operate/yx_xwlist');
            } else {
                $this->error('修改失败');
            }
        }
        $DLZL = $this->document->DocumentFind('xt_news', array(
            'id' => input('get.id')
        ));
        $this->assign('DLZL', $DLZL);
        return $this->fetch();
    }

    /**
     * 新闻排序
     */
    public function yx_xwpx()
    {
        $list = $this->document->DocumentList('xt_news', 'id desc');
        foreach ($list as $val) {
            $value = array(
                'paixu' => input('post.GGPX_' . $val['id'])
            );
            $this->document->EditDocument('xt_news', array(
                'id' => $val['id']
            ), $value);
        }
        $this->member_model->caozuorizhi_xieru('新闻排序');
        $this->redirect('/admin/operate/yx_xwlist');
    }

    /**
     * 新闻删除
     * 
     * @param 【优惠活动id】 $id            
     */
    public function yx_xwdel($id)
    {
        $news=Db::name('xt_news')->where('id',$id)->find();
        if ($this->document->DelAnnouncement('xt_news', $id)) {
             $this->member_model->caozuorizhi_xieru('删除新闻《'.$news['title'].'》');
            $this->success('删除成功');
        } else {
            $this->error('删除失败');
        }
    }

    /**
     * 新闻添加
     */
    public function yx_xwnew()
    {
        $time = date('Y-m-d H:i:s');
        $this->assign('time', $time);
        if (input('post.submit')) {
            $value = array(
                'type' => input('post.NEW_TYPE'),
                'title' => input('post.NEW_TITLE'),
                'ggtext' => input('post.NEW_GGTEXT'),
                'fatime' => strtotime(input('post.NEW_FATIME')),
                'faren' => input('cookie.user'),
                'xianshi' => 1,
                'paixu' => 0
            );
            if ($this->document->AddDocument('xt_news', $value)) {
                $this->member_model->caozuorizhi_xieru('添加新闻《'.input('post.NEW_TITLE').'》');
                $this->success('添加成功', '/admin/operate/yx_xwnew');
            } else {
                $this->error('添加失败');
            }
        } else {
            return $this->fetch();
        }
    }

    /**
     * APP下载地址
     * 
     * @return \think\mixed
     */
    public function yx_app()
    {
        $APP_URL = $this->document->DocumentFind('xt_app_url');
        $this->assign('app', $APP_URL);
        
        if (input('post.')) {

        	$file1 = $this->systemSet->uploadUrl('file_1');
        	$file2 = $this->systemSet->uploadUrl('file_2');
        	$file0 = $this->systemSet->uploadUrl('file_0');
        	if($file1==''){
        		$file1=input('post.az_img');
        	}
        	if($file2==''){
        		$file2=input('post.ios_img');
        	}
        	if($file0==''){
        		$file0=input('post.db_img');
        	}
            $value = array(
                'ios_url' => input('post.ios_url'),
                'az_url' => input('post.az_url'),
                'ios_img' => $file2,
                'az_img' => $file1,
            	'db_url' => input('post.db_url'),
            	'db_img' => $file0,
            );
            if (empty($APP_URL['id'])) {
                if ($this->document->AddDocument('xt_app_url', $value)) {
                    $this->member_model->caozuorizhi_xieru('添加APP二维码');
                    $this->success('添加成功', '/admin/operate/yx_app');
                } else {
                    $this->error('添加失败');
                }
            } else {
                if ($this->document->EditDocument('xt_app_url', array(
                    'id' => $APP_URL['id']
                ), $value)) {
                    $this->member_model->caozuorizhi_xieru('修改APP二维码');
                    $this->success('添加成功', '/admin/operate/yx_app');
                } else {
                    $this->error('添加失败');
                }
            }
        } else {
            return $this->fetch();
        }
    }

    /**
     * 安装教程
     */
    public function yx_appjc()
    {
        $APP_URL = $this->document->DocumentFind('xt_app_jc');
        $this->assign('APP_URL', $APP_URL);
        if (input('post.submit')) {
            $value = array(
                'img_1' => input('post.Android_EWM_1'),
                'img_2' => input('post.Android_EWM_2'),
                'img_3' => input('post.Android_EWM_3'),
                'img_4' => input('post.Android_EWM_4'),
                'img_5' => input('post.Android_EWM_5'),
                'img_6' => input('post.Android_EWM_6'),
                'img_7' => input('post.Android_EWM_7'),
                'img_8' => input('post.Android_EWM_8'),
                'img_9' => input('post.Android_EWM_9')
            );
            if (empty($APP_URL['id'])) {
                if ($this->document->AddDocument('xt_app_jc', $value)) {
                    $this->success('添加成功', '/admin/operate/yx_app');
                } else {
                    $this->error('添加失败');
                }
            } else {
                if ($this->document->EditDocument('xt_app_jc', array(
                    'id' => $APP_URL['id']
                ), $value)) {
                    $this->success('添加成功', '/admin/operate/yx_appjc');
                } else {
                    $this->error('添加失败');
                }
            }
        } else {
            return $this->fetch();
        }
    }

    /**
     * 备份客户
     * 
     * @return \think\mixed
     */
    public function yx_kehumobile()
    {
        
        if (input('post.')) {
            $list = Db::name('username')->field('id,username,mobile,qq,bankren')->where(array('nbzh' => 0,'swzh' => 0,'ifbf'=>0))->select();
            $bflist = array();
            foreach ($list as $val) {
                $lins = array();
                if ($val['mobile'] != '') {
                    $lins['id'] = $val['id'];
                    if (input('post.BF_USER') == 1)
                        $lins['username'] = $val['username'];
                    if ($val['mobile'] == '') {
                        $valMOBILE = '[手机空]';
                    } else {
                        $valMOBILE = $val['mobile'];
                    }
                    
                    if (input('post.BF_MOBILE') == 1) {
                        $lins['mobile'] = $valMOBILE;
                    }
                    if ($val['qq'] == '') {
                        $val['qq'] = '空';
                    }
                    if (input('post.BF_QQ') == 1) {
                        $lins['qq'] = $val['qq'];
                    }
                    if ($val['bankren'] == '') {
                        $val['bankren'] = '空';
                    }
                    if (input('post.BF_NAME') == 1) {
                        $lins['bankren'] = $val['bankren'];
                    }
                    $bflist[] = $lins;
                }
                $this->document->EditDocument('username',array('id'=>$val['id']),array('ifbf'=>1));
            }
            if (count($bflist) < 1) {
                alert_back('无客户资料进行备份');
            }else{
                $export = new Excel;
                $export_seve = $export->export($bflist,date('Y-m-d',time()));
                 $this->member_model->caozuorizhi_xieru('备份客户资料');
            }
        } else {
            return $this->fetch();
        }
    }

    /**
     * 自动活动大厅
     * 
     * @return \think\mixed
     */
    public function yx_zzhdlist()
    {
        $list = $this->document->DocumentList('xt_zzhd', 'id desc');
        $this->assign('list', $list);
        return $this->fetch();
    }

    /**
     * 修改活动状态
     * 
     * @param unknown $id            
     */
    public function yx_zzhdStatus($id)
    {
        $find = $this->document->DocumentFind('xt_zzhd', array('id' => $id));
        $ifopen = $find['ifopen'] == 1 ? 2 : 1;
        if($ifopen==1){$zhuangtai="开启";}
        if($ifopen==2){$zhuangtai="关闭";}
        $this->document->EditDocument('xt_zzhd', array('id' => $id), array('ifopen' => $ifopen));
        $this->member_model->caozuorizhi_xieru('设置自助活动“'.$find['hd_name'].'”的状态为:'.$zhuangtai);
        $this->redirect('/admin/Operate/yx_zzhdlist');
    }

    /**
     * 活动删除
     * 
     * @param 【活动id】 $id            
     */
    public function yx_zzhdDel($id)
    {   
        $zizhu=Db::name('xt_zzhd')->where('id',$id)->find();
        if ($this->document->DelAnnouncement('xt_zzhd', $id)) {
            $this->member_model->caozuorizhi_xieru('删除自助活动“'.$zizhu['hd_name'].'”');
            $this->success('删除成功');
        } else {
            $this->error('删除失败');
        }
    }


    public function yx_tjzzhd(){
        $id = input('id',0);
        if (input('post.submit')) {
        	$post = input('post.');
        	$hd_je = '';
        	$tj_czje = '';
        	$tj_tzje = '';
        	$data = array();
        	foreach ($post as $key=>$it){
        		if(strpos($key, 'hd_je')!==false){
        			$hd_je .= sprintf("%.2f",$it).'-';
        		}elseif(strpos($key, 'tj_czje')!==false){
        			$tj_czje .= sprintf("%.2f",$it).'-';
        		}elseif(strpos($key, 'tj_tzje')!==false){
        			$tj_tzje .= sprintf("%.2f",$it).'-';
        		}else{
        			$data[$key]=$it;
        		}
        	}
            $value = array(
                'hd_name' => $data['hd_name'],
                'hd_je' => $hd_je,
                'tjzq' => $data['tjzq'],
                'tj_czje' => $tj_czje,
                'tj_tzje' => $tj_tzje,
                'lq_count' => $data['lq_count'],
                'lq_zcount' => $data['lq_zcount'],
                'faadmin' => input('cookie.user'),
                'fatime' => strtotime(input('post.fatime')),
                'smwz' => $data['smwz']
            );
            if( $id ){
                if( $this->document->EditDocument('xt_zzhd', ['id'=>$id],$value )){
                     $this->member_model->caozuorizhi_xieru('修改自助活动“'.$data['hd_name'].'”');
                    $this->success('添加成功', '/admin/operate/yx_zzhdlist');
                } else {
                    $this->error('添加失败');
                }
            }else{
                $value['ifopen']  = 1;
                if ($this->document->AddDocument('xt_zzhd', $value)) {
                    $this->member_model->caozuorizhi_xieru('添加自助活动“'.$data['hd_name'].'”');
                    $this->success('添加成功', '/admin/operate/yx_zzhdlist');
                } else {
                    $this->error('添加失败');
                }
            }
        } else {
            $detail = Db::name('xt_zzhd')->find($id);
            $hd_je = $tj_czje = $tj_tzje = array();
            if( $detail ){
                $hd_je = explode('-',trim($detail['hd_je'],'-'));
                $tj_czje = explode('-',trim($detail['tj_czje'],'-'));
                $tj_tzje = explode('-',trim($detail['tj_tzje'],'-'));
            }
            $this->assign('id',$id);
            $this->assign('detail',$detail);
            $this->assign('hd_je',$hd_je);
            $this->assign('tj_czje',$tj_czje);
            $this->assign('tj_tzje',$tj_tzje);
            $this->assign('order',count($hd_je));
            $this->assign('time', empty($detail) ? date('Y-m-d H:i:s') : date('Y-m-d H:i:s',$detail['fatime']));
            return $this->fetch();
        }
    }

    public function yx_zzhdedit()
    {
        $time = date('Y-m-d H:i:s');
        $this->assign('time', $time);
        $id = input('get.id');
        if (input('post.submit')) {
            $value = array(
                'hd_name' => input('post.hd_name'),
                'hd_je' => input('post.hd_je'),
                'tjzq' => input('post.tjzq'),
                'tj_czje' => input('post.tj_czje'),
                'tj_txje' => input('post.tj_txje'),
                'tj_tzje' => input('post.tj_tzje'),
                'lq_count' => input('post.lq_count'),
                'lq_zcount' => input('post.lq_zcount'),
                'faadmin' => input('cookie.user'),
                'fatime' => strtotime(input('post.fatime')),
                'ifopen' => 1,
                'smwz' => input('post.smwz')
            );
            if ($this->document->EditDocument('xt_zzhd', array('id' => $id), $value)) {
                js_tishi('修改成功', '/admin/operate/yx_zzhdlist');
            } else {
                alert_back('修改失败');
            }
        } else {
            $find = $this->document->DocumentFind('xt_zzhd',array('id' => $id));
            $this->assign('find',$find);
            return $this->fetch();
        }
    }
    
    //红包等级列表
    public function bonus(){
        if(input('post.')){
            $data = array();
            $data['flip'] = input('post.flip');
            $data['start_time'] = input('post.start_time');
            $data['end_time'] = input('post.end_time');
            if(empty(strtotime($data['start_time'])) && empty(strtotime($data['end_time']))){
                alert_back('请填写完整时间段');
            }
            if(strtotime($data['end_time']) < strtotime($data['start_time'])){
                alert_back('结束时间不能比开奖时间小');
            }else{
                $this->document->AddFindDocument('bonus_lay', $data);
                if($data['flip']==1){$type="存款红包";}
                if($data['flip']==2){$type="等级红包";}
                if($data['flip']==0){$type="关";}
                $this->member_model->caozuorizhi_xieru('设置红包开始时间为:'.$data['start_time'].'，结束时间为:'.$data['end_time'].'，红包类型为:'.$type);
                $this->redirect('bonus');
            }
            
        }else{
            $bonus_lay = $this->document->DocumentFind('bonus_lay');
            $list = $this->document->DocumentList('bonus_num', 'saving asc');
            $this->assign('bonus_lay',$bonus_lay);
            $this->assign('list',$list);
            return $this->fetch();
        }
        
    }
    
    //存款红包
    public function bonusDeposit(){
        
        $bonus_list = $this->document->DocumentList('bonus', 'lv asc');
        $this->assign('list',$bonus_list);
        return $this->fetch('bonus_deposit');
        
    }
    
    //红包等级添加
    public function bonusAdd(){
        
        if(input('post.')){
            $data = array();
            $data['lv'] = input('post.lv');
            $data['min_saving'] = input('post.min_saving');
            $data['max_saving'] = input('post.max_saving');
            $find = Db::name('bonus')->where('lv',$data['lv'])->find();
            if(count($find) > 0){
                alert_back('该等级已存在');
            }
            $insert = $this->document->AddDocument('bonus', $data);
            if($insert){
                $this->member_model->caozuorizhi_xieru('新增存款红包等级');
                js_tishi('添加成功！', 'bonusDeposit');
            }else{
                alert_back('添加失败！');
            }
        }else{
            return $this->fetch('bonus_add');
        }
        
    }
    
    
    //红包等级修改
    public function bonusEdit(){
        $id = input('get.id');
        if(input('post.')){
            $data = array();
            $data['lv'] = input('post.lv');
            $data['min_saving'] = input('post.min_saving');
            $data['max_saving'] = input('post.max_saving');
            $edit = $this->document->EditDocument('bonus', array('id' => $id),$data);
            if($edit){
                $this->member_model->caozuorizhi_xieru('修改存款红包等级，ID为:'.$id);
                js_tishi('修改成功！', 'bonusDeposit');
            }else{
                alert_back('修改失败！');
            }
        }else{
            $find = $this->document->DocumentFind('bonus',array('id' => $id));
            $this->assign('find',$find);
            return $this->fetch('bonus_edit');
        }
    
    }
    
    //红包等级删除
    public function bonusDel(){
        $id = input('get.id');
        $find = Db::name('bonus_chance')->where('lv',input('get.lv'))->count();
        if($find > 0){
            alert_back('改等级包含概率，请先删除该等级概率');
        }
        $del = $this->document->DelAnnouncement('bonus',$id);
        
        if($del){
            $this->member_model->caozuorizhi_xieru('删除存款红包等级，ID为:'.$find['lv']);
            js_tishi('删除成功！', 'bonusDeposit');
        }else{
            alert_back('删除失败！');
        }
        
    }
    
    //红包概率列表
    public function bonusChance(){
        if(input('get.type') == 'class'){
            $list = Db::name('bonus_chance')->field('id,chance_money,lv,min_money,max_money,chance')->where(array('type'=>2))->select();
            $class = Db::name('us_class')->column('id,class_name');
            $this->assign('class',$class);
        }else if(input('get.type') == 'saving'){
            $lv = input('get.lv');
            $list = $this->document->DocumentList('bonus_chance', 'lv asc',array('lv'=>$lv));
        }
        $this->assign('list',$list);
        $this->assign('lv',input('get.lv'));
        return $this->fetch('bonus_chance');
    }
    
    
    //红包概率添加
    public function bonusChanceAdd(){
        if(input('post.')){
            $data = array();
            $data['lv'] = input('post.lv');
            $data['chance'] = input('post.chance');
            $data['min_money'] = input('post.min_money');
            $data['max_money'] = input('post.max_money');
            $data['chance_money'] = input('post.chance_money') && input('get.type') == 'class' ? input('post.chance_money') : 0;
            $data['type'] = input('get.type') == 'class' ? 2 : 1;
            
            if($data['type'] == 2){
                $find = Db::name('bonus_chance')->where(array('lv'=>$data['lv'],'type'=>2))->find();
                if(count($find) > 0){
                    $insert = $this->document->EditDocument('bonus_chance', array('lv'=>$data['lv']), $data);
                }else{
                    $insert = $this->document->AddDocument('bonus_chance', $data);
                }
            }else{
                $insert = $this->document->AddDocument('bonus_chance', $data);
            }
            
            if($insert){
                $find=Db::name('bonus_chance')->order('id desc')->find();
                $this->member_model->caozuorizhi_xieru('新增红包概率，ID为:'.$find['id']);
                js_tishi('添加成功！', 'bonusChance?type='.input('get.type').'&lv='.$data['lv']);
            }else{
                alert_back('添加失败！');
            }
        }else{
            if(input('get.type') == 'class'){
                $bonus_lv = $this->document->DocumentList('us_class', 'id asc');
            }else{
                $bonus_lv = $this->document->DocumentList('bonus', 'lv asc');
            }
            
            $list = $this->document->DocumentList('bonus_chance', 'lv asc');
            $this->assign('list',$list);
            $this->assign('bonus_lv',$bonus_lv);
            return $this->fetch('bonus_chance_add');
        }
        
    }
    


    //红包概率修改
    public function bonuschanceEdit(){
        $id = input('get.id');
        if(input('post.')){
            $data = array();
            $data['lv'] = input('post.lv');
            $data['chance'] = input('post.chance');
            $data['min_money'] = input('post.min_money');
            $data['max_money'] = input('post.max_money');
            $edit = $this->document->EditDocument('bonus_chance', array('id' => $id),$data);
            if($edit){
                $this->member_model->caozuorizhi_xieru('修改存款红包概率，ID为:'.$id);
                js_tishi('修改成功！', 'bonusChance?type=saving&lv='.input('lv'));
            }else{
                alert_back('修改失败！');
            }
        }else{
            $bonus_lv = $this->document->DocumentList('bonus', 'lv asc');
            $find = $this->document->DocumentFind('bonus_chance',array('id' => $id));
            $this->assign('find',$find);
            $this->assign('bonus_lv',$bonus_lv);
            return $this->fetch('bonus_chance_edit');
        }
    
    }
    

    //红包概率删除
    public function bonusChanceDel(){
        $id = input('get.id');
        $data=Db::name('bonus_chance')->where('id',$id)->find();
        $del = $this->document->DelAnnouncement('bonus_chance',$id);
        if($del){
            $this->member_model->caozuorizhi_xieru('删除红包概率');
            js_tishi('删除成功！', 'bonusChance?type='.input('get.type').'&lv='.input('lv'));
        }else{
            alert_back('删除失败！');
        }
    
    }
    

    //红包概率列表
    public function bonusNum(){
        $list = $this->document->DocumentList('bonus_num', 'saving asc');
        $this->assign('list',$list);
        return $this->fetch('bonus_num');
    }
    
    


    //抢红包次数添加
    public function bonusNumAdd(){
        if(input('post.')){
            $data = array();
            if(input('post.num')<=999){
	            $data['saving'] = input('post.saving');
	            $data['num'] = input('post.num');
	            $insert = $this->document->AddDocument('bonus_num', $data);
	            if($insert){
                    $this->member_model->caozuorizhi_xieru('新增抢红包次数');
	                js_tishi('添加成功！', 'bonus');
	            }else{
	                alert_back('添加失败！');
	            }
            }else{
            	alert_back('次数最多不超过999次');
            }
        }else{
            return $this->fetch('bonus_num_add');
        }
    
    }
    
    


    //红包次数修改
    public function bonusNumEdit(){
        $id = input('get.id');
        if(input('post.')){
            $data = array();
            $data['saving'] = input('post.saving');
            $data['num'] = input('post.num');
            $edit = $this->document->EditDocument('bonus_num', array('id' => $id),$data);
            if($edit){
                $this->member_model->caozuorizhi_xieru('修改抢红包次数，ID为:'.$id);
                js_tishi('修改成功！', 'bonus');
            }else{
                alert_back('修改失败！');
            }
        }else{
            $find = $this->document->DocumentFind('bonus_num',array('id' => $id));
            $this->assign('find',$find);
            return $this->fetch('bonus_num_edit');
        }
    
    }
    
    

    //红包概率删除
    public function bonusNumDel(){
        $id = input('get.id');
        $del = $this->document->DelAnnouncement('bonus_num',$id);
        if($del){
            $this->member_model->caozuorizhi_xieru('删除抢红包次数');
            js_tishi('删除成功！', 'bonus');
        }else{
            alert_back('删除失败！');
        }
    
    }
    
    
}